import {
  SET_FULL_PLAYER,
  SET_MINI_PLAYER,
  SET_PLAYING,
  SET_MODETYPE,
  SONG_TYPE,
  SET_SONG_LYRIC,
  DEL_SINGAL_SONG,
  SET_CURRENT_INDEX,
  SET_PLAYING_TIME,
  SET_FAVORITE_SONG,
  SET_FAVORITE_LIST,
  SET_HISTORY_LIST,
  SET_HISTORY_SONG
} from './mutations-type'
export default {
  [SET_FULL_PLAYER] (state, flag) {
    state.isFullPlayer = flag
  },
  [SET_MINI_PLAYER] (state, flag) {
    state.isMiniPlayer = flag
  },
  [SET_PLAYING] (state, flag) {
    state.isPlaying = flag
  },
  [SET_MODETYPE] (state, flag) {
    state.modeType = flag
  },
  [SONG_TYPE] (state, list) {
    state.songs = list
  },
  [SET_SONG_LYRIC] (state, lyric) {
    state.currentLyric = lyric
  },
  [DEL_SINGAL_SONG] (state, index) {
    if (index !== undefined) {
      if (index < state.currentIndex) {
        state.songs.splice(index, 1)
        state.currentIndex = state.currentIndex - 1
      } else {
        state.songs.splice(index, 1)
      }
    } else {
      state.songs = []
    }
    if (state.songs.length === 0) {
      state.isFullPlayer = false
      state.isMiniPlayer = false
      state.isPlaying = false
      state.currentSong = {}
      state.currentIndex = 0
      state.currentLyric = {}
    }
  },
  [SET_CURRENT_INDEX] (state, index) {
    if (index < 0) {
      index = state.songs.length - 1
    } else if (index > state.songs.length - 1) {
      index = 0
    }
    state.currentIndex = index
  },
  [SET_PLAYING_TIME] (state, time) {
    state.playingTime = time
  },
  [SET_FAVORITE_SONG] (state, song) {
    const result = state.favoriteList.find(function (value) {
      return value.id === song.id
    })
    if (result === undefined) {
      state.favoriteList.push(song)
    }
  },
  [SET_FAVORITE_LIST] (state, list) {
    state.favoriteList = list
  },
  [SET_HISTORY_LIST] (state, list) {
    state.historyList = list
  },
  [SET_HISTORY_SONG] (state, song) {
    const result = state.historyList.find(function (value) {
      return value.id === song.id
    })
    if (result === undefined) {
      state.historyList.push(song)
    }
  }
}
